package net.dfrz.oa.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import net.dfrz.oa.action.support.BaseAction;
import net.dfrz.oa.dto.UserDto;
import net.dfrz.oa.dto.UserDto2;
import net.dfrz.oa.pojo.Autority;
import net.dfrz.oa.pojo.OneCategory;
import net.dfrz.oa.pojo.Role;
import net.dfrz.oa.pojo.User;
import net.dfrz.oa.service.AutorityService;
import net.dfrz.oa.service.NoticeService;
import net.dfrz.oa.service.OneCategoryService;
import net.dfrz.oa.service.RoleService;
import net.dfrz.oa.service.UserService;

@Controller
@Scope("prototype")
@Namespace("") //命名空间
@Results({
	@Result(name="false",location="/login.jsp"),
	@Result(name="success",location="main.jsp"),
	@Result(name="logout",location="/login.jsp"),
	@Result(name="myinfo",location="myinfo.jsp"),
	@Result(name="repwd",location="repwd.jsp"),
	@Result(name="userList2", type="json", params={"root","list"})
})
public class UserAction extends BaseAction<User> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private User user;	
	
	private UserDto userDto;
	
	private UserDto2 userDto2;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private AutorityService autorityService;
	@Autowired
	private OneCategoryService oneCategoryService;
	@Autowired
	private NoticeService noticeService;
	
	private String imageCode;//验证码 
	
	private List<User> list = null;
	public String getImageCode() {
		return imageCode;
	}

	public void setImageCode(String imageCode) {
		this.imageCode = imageCode;
	}

	public UserDto getUserDto() {
		return userDto;
	}

	public void setUserDto(UserDto userDto) {
		this.userDto = userDto;
	}

	public UserDto2 getUserDto2() {
		return userDto2;
	}

	public void setUserDto2(UserDto2 userDto2) {
		this.userDto2 = userDto2;
	}

	public List<User> getList() {
		return list;
	}

	public void setList(List<User> list) {
		this.list = list;
	}

	/**
	 * 模型驱动
	 */
	@Override
	public User getModel() {
		user = new User();
		return user;
	}
	/**
	 * 登录
	 */
	@Action("user_login")  //oa/login.action
	public String login(){
		String code = (String) getSession().getAttribute("sRand");
		if(imageCode==null || code == null){
			return "false";
		}
		if(!imageCode.equals(code)){
			getRequest().setAttribute("error", "验证码错误");
			return "false";
		}
		User u = userService.login(user);
		if(u==null){
			getRequest().setAttribute("error", "账号或密码错误");
			return "false";
		}
		
		//查询用户在数据库拥有的菜单存放到session中 
		//1.当前用户的角色
		Role role = u.getRole();
		//System.out.println(role);
		//2.根据角色查询用户拥有的权限,返回数据库对应的对象
		Integer rid = role.getId();
		Role r = roleService.getRoleById(rid);
		//System.out.println(r);
		Set<Autority> auto = r.getAutorities();
		//3.根据返回的权限返回父节点即对应一级菜单
		Set<Integer> ids = new HashSet<Integer>();
		for(Autority a : auto){
			Integer aid = a.getPid();
			ids.add(aid);
		}
		List<Autority> fList =autorityService.findAllById(ids);
		//4.根据父节点查询出一级菜单
		List<OneCategory> ocList = new ArrayList<OneCategory>();
		for(Autority f:fList){
			Integer faid = f.getId();
			OneCategory oc = oneCategoryService.getByaid(faid);
			ocList.add(oc);
		}
		//5.将一级菜单放入session中
		getSession().setAttribute("oneCategory", ocList);
		//获取系统用户对应的通知存入session
		getSession().setAttribute("userNotify", u.getSystem());
		//获取系统用户对应的通知是否为0
		getSession().setAttribute("notifySize", u.getSystem().size());
		//获取系统公告
		getSession().setAttribute("notice", noticeService.getByNid(1));
		//登录成功将数据库中的用户存在session中
		getSession().setAttribute("currentUser", u);
		
		//登录成功返回欢迎页
//		//return "success";		
		return "success";
	}
	
	/**
	 * 退出登录
	 */
	@Action("user_logout")
	public String logOut(){
		getSession().invalidate();
		return "logout";
	}
	
	/**
	 * 查询出所有用户
	 */
	@Action("user_findAll")
	public String findAll(){
		list = userService.findAll();
		return "userList2";
	}
	
	/**
	 * 个人信息管理
	 */
	@Action("user_infoManage")
	public String infoManage(){	
		return "myinfo";
	}
	
	/**
	 * 更新个人信息
	 */
	@Action("user_updateUser")
	public void updateUser() throws Exception{
		userService.updateUser(userDto);
		//更新完 需修改在session中的user
		User user = (User) getSession().getAttribute("currentUser");
		user.setId(userDto.getUserId());
		user.setName(userDto.getName());
		user.setMajor(userDto.getMajor());
		user.setEdu(userDto.getEdu());
		user.setSchool(userDto.getSchool());
		user.setSex(userDto.getSex());
		getResponse().getWriter().write("true");
	}
	
	/**
	 * 修改密码初始页面
	 */
	@Action("user_repwdManage")
	public String repwdManage(){
		return "repwd";
	}
	
	/**
	 * 校验原始密码准确性
	 * @throws Exception 
	 */
	@Action("user_checkPwd")
	public void checkPwd() throws Exception{
		String oldPwd = getRequest().getParameter("oldPwd");
		//获取当前session中user的pwd
		User user =(User) getSession().getAttribute("currentUser");
		String userPwd = user.getPassword();
		PrintWriter write = getResponse().getWriter();
		if(userPwd.equals(oldPwd)){
			write.write("1");
		}else{
			write.write("");
		}
	}
	
	/**
	 * 修改当前用户的密码
	 */
	@Action("user_passwordEdit")
	public void passwordEdit() throws Exception{
		Integer uid = ((User)getSession().getAttribute("currentUser")).getId();
		String newPwd = userDto2.getNewPwd();
		//执行修改
		userService.passwordEdit(uid,newPwd);
		getResponse().getWriter().write("1");
	}
}
